import 'package:syria_sports/core/app_export.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../add_gym_page/bloc/add_gym_page_screen.dart';
import '../widgets/gym_item_view.dart';
import 'gyms_cubit.dart';
import 'package:flutter/material.dart';

class MyGymsScreen extends StatelessWidget {
  const MyGymsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    var gymsCubit =GymsCubit();
    return Scaffold(
      appBar:_buildAppBar(context:context ,cubit: gymsCubit),
      body: BlocProvider<GymsCubit>(
        create: (context) =>gymsCubit ..init(),
        child: BlocConsumer<GymsCubit, GymsState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = GymsCubit.get(context);
            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isDone: state is GymsDone,
                isError: state is GymsError,
                isLoading: state is GymsLoading,
                onRefresh: cubit.getAllGyms,
                onDone: GridView.builder(
                  gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                      maxCrossAxisExtent: 410,
                      mainAxisExtent: 250
                  ),
                  itemCount: cubit.gyms.length,
                  itemBuilder: (context, index) {
                    return MyGymItemView(gym:cubit.gyms[index]);
                  },
                ),
                /*onDone: ListView.separated(
                  separatorBuilder: (_, index){
                    return const Divider(color: Colors.red,);
                  },
                  itemCount:cubit.gyms.length,
                  itemBuilder: (context,index){
                    return GymItemView(gym:cubit.gyms[index]);
                  },
                ),*/
              ),

            );
          },
        ),
      ),
    );
  }

  /// Section Widget
  PreferredSizeWidget _buildAppBar({required BuildContext context, required GymsCubit cubit}) {
    return CustomAppBar(
        actions: [
          if(PrefUtils.isGymAdmin())
          OutlineIconButton(
            icon: Icons.add,
            margin:const EdgeInsets.all(8),
            padding:const EdgeInsets.all(2),
            onTapped: ()async{
              await Navigator.push(context, MaterialPageRoute(builder: (context) => const AddGymPageScreen()));
            },
          ),
        ],
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "النوادي", margin: EdgeInsets.only(left: 12.h),
        ),
    );
  }
}

